package com.duoketech.controller.web.product;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.modulepic.ModulePic;
import com.duoketech.model.product.Product;
import com.duoketech.service.web.ModulePicService;
import com.duoketech.service.web.product.ProductService;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.upload.QiNiuUpload;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.qiniu.common.QiniuException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ProductController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<Product> result = (Page<Product>) ProductService.ME.pageAllRecord(getModel(Product.class),pageNum,pageSize,kv);

        for(Product p:result.getList()){
            p.set(Product.IS_POST,p.is_post());
            p.set(Product.IS_SALE,p.is_sale());
            p.set(Product.STATUS,p.status());
            p.set(Product.PRICE,p.price());
            p.set(Product.WEIGHT,p.weight());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {

            Product p = (Product) ProductService.ME.queryBy(id,getModel(Product.class),Kv.create());
            p.set(Product.PRICE,p.price());
            p.set(Product.WEIGHT,p.weight());
            p.set(Product.COST_PRICE,p.costPrice());
            setAttr("product",p);
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            Product product = getModel(Product.class);
            String price = getPara("price","1.0");
            String cost_price = getPara("cost_price","1.0");
            String weight = getPara("weight","1.0");
            product.set(Product.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            product.set(Product.MEMBER_ID,getShopMemberId());
            product.set(Product.COST_PRICE,PageUtil.yuanToFeng(Double.valueOf(cost_price)));
            product.set(Product.WEIGHT,PageUtil.kg2G(Double.valueOf(weight)));
            ProductService.ME.doAdd(product,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            Product product = getModel(Product.class);
            String price = getPara("price","1.0");
            String cost_price = getPara("cost_price","1.0");
            String weight = getPara("weight","1.0");
            product.set(Product.PRICE,PageUtil.yuanToFeng(Double.valueOf(price)));
            product.set(Product.MEMBER_ID,getShopMemberId());
            product.set(Product.COST_PRICE,PageUtil.yuanToFeng(Double.valueOf(cost_price)));
            product.set(Product.WEIGHT,PageUtil.kg2G(Double.valueOf(weight)));
            ProductService.ME.update(product,Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        ProductService.ME.deleteBy(id,getModel(Product.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }

  }